package com.corpgovernment.common.helper;



import org.springframework.util.StringUtils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: corpgovernment-common-logging
 * @description: request请求获取ip
 * @author: wubing
 * @create: 2019-06-10 16:06
 */
public class RequestHelper {
    private RequestHelper() {
    }

    public static String getClientIPAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static Map<String, String> getAllHeaders(HttpServletRequest request) {
        Map<String, String> mapResult = new HashMap();
        Enumeration<String> tmpHeadNames = request.getHeaderNames();
        if (tmpHeadNames == null) {
            return mapResult;
        } else {
            Enumeration e = tmpHeadNames;

            while (e.hasMoreElements()) {
                String thisName = e.nextElement().toString();
                String thisValue = request.getHeader(thisName);
                mapResult.putIfAbsent(thisName, thisValue);
            }

            return mapResult;
        }
    }

    public static String getRawUrl(HttpServletRequest request) {
        String uri = request.getRequestURI();
        Enumeration enumeration = request.getParameterNames();
        StringBuilder sb = new StringBuilder();

        while (enumeration.hasMoreElements()) {
            String paramName = (String) enumeration.nextElement();
            String paramValue = request.getParameter(paramName);
            sb.append(paramName).append("=").append(paramValue).append("&");
        }

        if (!StringUtils.isEmpty(sb.toString())) {
            uri = uri + "?" + removeEnd(sb.toString(), "&");
        }

        return uri;
    }

    private static String removeEnd(String str, String remove) {
        if (!StringUtils.isEmpty(str) && !StringUtils.isEmpty(remove)) {
            return str.endsWith(remove) ? str.substring(0, str.length() - remove.length()) : str;
        } else {
            return str;
        }
    }

    public static String getActionName(HttpServletRequest request) {
        String[] requests = request.getRequestURI().split("/");
        return requests[requests.length - 1];
    }

    /**
     * 拿取请求body参数
     *
     * @param request
     * @return
     */
    public static String getRequestParams(HttpServletRequest request) {
        int len = request.getContentLength();
        if (len < 0) {
            return "";
        }
        String requestStr = "";
        String requestType = request.getMethod();
        if ("get".equalsIgnoreCase(requestType)) {
            requestStr = request.getQueryString();
        } else if ("post".equalsIgnoreCase(requestType)) {
            ServletInputStream stream;
            byte[] buffer = new byte[len];
            try {
                stream = request.getInputStream();
                stream.read(buffer, 0, len);
                requestStr = new String(buffer, "utf-8");
            } catch (Exception ex) {
            }
        }
        return requestStr;
    }


}
